#include "StdAfx.h"
#include "../Engine/TerrainTile.hpp"
#include "../Engine/UnitRenderer.hpp"
#include "../Engine/Mercenary.hpp"
#include "../Engine/UnitRendererManager.hpp"

namespace he
{
	UnitRendererManager::UnitRendererManager(boost::filesystem::path const &WrkPath, Ogre::SceneManager &SceneManager)
		: m_WrkPath(WrkPath), m_UnitManager(WrkPath), m_SceneManager(SceneManager)
	{
	}

	UnitRendererPtr_t UnitRendererManager::create_afv(std::string const &Side, std::string const &UnitType, TerrainTile const &InitialPosition)
	{
		// Create unit
		UnitPtr_t p_unit = m_UnitManager.load_afv_unit(Side, UnitType);
		// Create entity
		Ogre::Entity *p_entity = m_SceneManager.createEntity(UnitType + ".mesh");
		// Add as child node
//		Ogre::SceneNode *p_node = m_SceneManager.getRootSceneNode()->createChildSceneNode(Ogre::Vector3(*InitialPosition.get_vertex(1)));
		Ogre::SceneNode *p_node = m_SceneManager.getRootSceneNode()->createChildSceneNode(Ogre::Vector3(6.f, 15.f, 24.f));
		p_node->attachObject(p_entity);

		return UnitRendererPtr_t(new UnitRenderer(p_unit, p_entity, p_node));
	}

	std::unique_ptr<MercenaryController> UnitRendererManager::create_mercenary(std::string const &Mesh, TerrainTile const *pTile)
	{
		// Load model as first
		Ogre::MeshPtr p_mesh = Ogre::MeshManager::getSingleton().load(Mesh, Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME);
		// Create entity
		Ogre::Entity *p_entity = m_SceneManager.createEntity(p_mesh);
		p_entity->setQueryFlags(0);
		// Set animation blend mode
		p_entity->getSkeleton()->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE);
		// Add as child node
		Ogre::SceneNode *p_node = m_SceneManager.getRootSceneNode()->createChildSceneNode(Ogre::Vector3(0, 0, 0));
		p_node->setOrientation(Ogre::Vector3(0, 0, 1).getRotationTo(Ogre::Vector3(.5f, 0, .5f)));
		p_node->attachObject(p_entity);
		p_node->showBoundingBox(true);

		MercenaryPtr_t p_unit(new Mercenary("aaaa", pTile, LD_SOUTHEAST));

		return std::unique_ptr<MercenaryController>(new MercenaryController(p_unit, UnitRendererPtr_t(new UnitRenderer(p_unit, p_entity, p_node))));
	}

} /*he*/